package com.linkage.usercenter.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.linkage.common.constant.CommonSysConstant;
import com.linkage.common.exception.ServiceException;
import com.linkage.usercenter.dao.CoreNoticeMapper;
import com.linkage.usercenter.pojo.entity.CoreNotice;
import com.linkage.usercenter.service.INoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 通知表 服务实现类
 * </p>
 *
 * @author shibiao
 * @since 2020-06-08
 */
@Service
public class NoticeServiceImpl implements INoticeService {


    @Autowired
    private CoreNoticeMapper coreNoticeMapper;


    @Override
    public void createNotice(CoreNotice coreNotice) throws Exception {
        coreNotice.setId(IdUtil.simpleUUID());
        coreNotice.setCreatetime(new Date());
        this.coreNoticeMapper.insert(coreNotice);
    }

    @Override
    public CoreNotice singleNoticeShow(String id) throws Exception {
        CoreNotice coreNotice = this.coreNoticeMapper.selectByPrimaryKey(id);
        return coreNotice;
    }

    @Override
    public PageInfo<CoreNotice> listNoticeShow(Integer pageNo, Integer pageSize, String title, Integer type) throws Exception {
        if(null == pageNo){
            pageNo = CommonSysConstant.DEFAULT_PAGE_NO;
        }
        if(null == pageSize){
            pageSize = CommonSysConstant.DEFAULT_PAGE_SIZE;
        }
        PageHelper.startPage(pageNo, pageSize);
        List<CoreNotice> eventMessages = this.coreNoticeMapper.selectByNoticeTitleAndType(type, title);
        return new PageInfo<CoreNotice>(eventMessages);
    }

    @Override
    public List<CoreNotice> queryList(CoreNotice notice) {
        if(ObjectUtil.isNull(notice)){
            notice = new CoreNotice();
        }
        notice.setStatus(1);
        return coreNoticeMapper.queryList(notice);
    }

    @Override
    public Integer deleteNotice(String id) throws Exception {
        return this.coreNoticeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer updateNotice(CoreNotice notice) throws Exception {
        return this.coreNoticeMapper.updateByPrimaryKeySelective(notice);
    }
}
